//
//  UIColor+xsHexColor.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/7.
//

#import "UIColor+xsHexColor.h"

@implementation UIColor (xsHexColor)

+ (UIColor *)xsColorwithHexString:(NSString *)color Alpha:(CGFloat)alpha {
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }

    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 8 && [cString length]!=6)
        return [UIColor clearColor];

    CGFloat insideAlpha = 1.0f;
    if ([cString length]==8) {
        NSString *aString = [cString substringWithRange:NSMakeRange(0, 2)];
        unsigned int a;
        [[NSScanner scannerWithString:aString] scanHexInt:&a];
        insideAlpha = (float)a / 255.0f;
        cString =  [cString substringWithRange:NSMakeRange(2, 6)];
    }
    if (alpha>0) {
        insideAlpha = alpha;
    }

    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;

    //r
    NSString *rString = [cString substringWithRange:range];

    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:insideAlpha];
}


+ (NSArray *)get84Color{
    
    
    NSArray *tempColorArr = @[
        @"#ff0000",
        @"#ff7f00",
        @"#ffff00",
        @"#00ff00",
        @"#0000ff",
        @"#4b0082",
        @"#9400d3",
        @"#ff1493",
        @"#ff69b4",
        @"#ffc0cb",
        @"#ffa500",
        @"#ffd700",
        @"#ffffff",
        @"#000000",
      ];
    
//    NSInteger temCount = 0;
    NSMutableArray *tempColorMarr = [NSMutableArray new];
    for (int i=0; i<tempColorArr.count; i++) {
        NSString *tempColorStr = tempColorArr[i];
        
        NSString *redStr = [tempColorStr substringWithRange:NSMakeRange(1, 2)];
        NSString *gStr = [tempColorStr substringWithRange:NSMakeRange(3, 2)];
        NSString *bStr = [tempColorStr substringWithRange:NSMakeRange(5, 2)];
        
        
        int rInt = [self xsHexStringToDecimal:redStr];
        int gInt = [self xsHexStringToDecimal:gStr];
        int bInt = [self xsHexStringToDecimal:bStr];
        
//        NSLog(@"rInt==%d\n",rInt);
//        NSLog(@"gInt==%d\n",gInt);
//        NSLog(@"bInt==%d\n",bInt);
        
        int asdd=10;
        for (CGFloat j=0.0; j<=0.5; j+=0.1) {
            CGFloat rFloat = 255.0 * j + rInt*1.0 * (1.0-j);
            CGFloat gFloat = 255.0 * j + gInt*1.0 * (1.0 - j);
            CGFloat bFloat = 255.0 * j + bInt*1.0 * (1.0 - j);
            
            
            int rInt = rFloat;
            int gInt = gFloat;
            int bInt = bFloat;
            
           NSString *rFinalStr = [self hexStringFromDecimal:rInt];
            NSString *gFinalStr = [self hexStringFromDecimal:gInt];
            NSString *bFinalStr = [self hexStringFromDecimal:bInt];
            
            NSString *hColorFinalStr = [NSString stringWithFormat:@"#%@%@%@",rFinalStr,gFinalStr,bFinalStr];
            [tempColorMarr addObject:hColorFinalStr];
//            NSLog(@"rFloat==%f\n",rFloat);
//            NSLog(@"gFloat==%f\n",gFloat);
//            NSLog(@"bFloat==%f\n",bFloat);
            
//            temCount+=1;
        }
        
    }
    //NSLog(@"temCount==%ld\n",(long)temCount);
    
//    NSString *tem16 = [self xsTenToHex:9];
//    NSString *tem162 = [self xsTenToHex:10];
//    NSString *tem163 = [self xsTenToHex:11];
    
    return [NSArray arrayWithArray:tempColorMarr];
}

//在这个方法中，%02X格式说明符意味着：
//%：表示格式化输出
//0：表示不足位数的部分用0来填充
//2：表示总共输出2位字符
//X：表示以大写的十六进制形式输出
//使用这个方法，你可以将任何十进制数值转换为至少两位的十六进制字符串。例如：
//NSInteger decimalNumber = 5;
//NSString *hexString = [self hexStringFromDecimal:decimalNumber];
//NSLog(@"Hex String: %@", hexString); // 输出: Hex String: 05
//如果decimalNumber是258，则输出将是"010A"。
+ (NSString *)hexStringFromDecimal:(NSInteger)decimal {
    NSString *hexString = [NSString stringWithFormat:@"%02X", decimal];
    return hexString;
}


#pragma mark 将两位十六进制字符转成十进制数字
+ (int)xsHexStringToDecimal:(NSString *)hexString {
    
    
    unsigned int outVal;
    NSScanner* scanner = [NSScanner scannerWithString:hexString];
    [scanner scanHexInt:&outVal];
    //NSLog(@"%d", outVal);

    return outVal;
}
//十进制到16进制的转换
+ (NSString *)xsTenToHex:(long long int)tmpid
{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i = 0; i<9; i++) {
     ttmpig=tmpid%16;
    tmpid=tmpid/16;
    switch (ttmpig)
    {
        case 10:
            nLetterValue =@"A";break;
        case 11:
            nLetterValue =@"B";break;
        case 12:
            nLetterValue =@"C";break;
        case 13:
            nLetterValue =@"D";break;
        case 14:
            nLetterValue =@"E";break;
        case 15:
            nLetterValue =@"F";break;
        default:nLetterValue=[[NSString alloc]initWithFormat:@"%i",ttmpig];
            
    }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }

    }
    return str;
}

// 颜色转成图片
+ (UIImage *)xsColorAsImage:(UIColor *)color withImaegSize:(CGSize)ImgSize{
    
    CGRect rect = CGRectMake(0, 0, ImgSize.width, ImgSize.height);
    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,color.CGColor);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
